/**
 * 组合促销价格维护 - 编辑页面
 * @author shixc
 * @date 2019/01/28
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            /*gwdc027: false,*/
            gwdc020: true,
            gwdc021: true,
            gwdcz: true,
            gwdc007: false,
            gwdc025: true,
            gwdc026: true,
            detail: false,
            GWDC022: false,
            hide: false,
            form: '',
            rules: {
                GWDC001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC009: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWDC010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                gwdc005Options: app.datas_('crm_gwcd_gwcd005'),
                gwdc006Options: app.datas_('crm_gwcd_gwcd006'),
                gwdc007Options: app.datas_('crm_gwcd_gwcd007'),
                gwdc022Options: app.datas_('crm_gwai501_gwax011')
            }
        },
        cols1 = [[
            {field: 'GWDC002', type: 'checkbox', title: '选择'}
            , {field: 'GWDC016', width: 80, title: '客户编号', event: 'openKh'}
            , {field: 'GWDC017', width: 80, title: '客户名称'}
            , {field: 'GWDC018', width: 120, title: '品号', event: 'openGwam'}
            , {field: 'GWAML003', width: 120, title: '品名'}
            , {field: 'GWAML004', width: 120, title: '规格'}
            , {field: 'GWDC019', width: 80, title: '单位说明'}
            , {field: 'GWDC023', width: 80, title: '结算价'}
            , {field: 'GWDC024', width: 80, title: '活动价格', edit: 'text'}
        ]],
        cols2 = [[
            {field: 'GWDCZ002', type: 'checkbox', title: '选择'}
            , {field: 'GWDCZ003', width: 80, title: '序号'}
            , {field: 'GWDCZ004', width: 80, title: '赠品品号', event: 'openGwdcz'}
            , {field: 'GWAML003', width: 120, title: '赠品品名'}
            , {field: 'GWAML004', width: 120, title: '赠品规格'}
            , {field: 'GWDCZ005', width: 80, title: '赠品价格', edit: 'text'}
            , {field: 'GWDCZ006', width: 80, title: '赠品结算价', hide: true}
        ]],
        cols3 = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWDCT002', width: 80, title: '序号'}
            , {field: 'GWDCT003', width: 120, title: '起订数量', edit: 'text'}
            , {field: 'GWDCT004', width: 120, title: '起订金额', edit: 'text'}
            , {field: 'GWDCT005', width: 120, title: '最大赠品数量', edit: 'text'}
            , {field: 'GWDCT006', width: 120, title: '最大赠品金额', edit: 'text'}
        ]],
        callObj,
        moduleName,
        listData = null, upload, table_where = {},
        table, gwdc, gwdc004Table, gwdczTable, gwdctTable, layOpenIndex,pageVue
    ;

    var Gwdc004Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwdc004_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }]
                }, win);
            },
            initTable1: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdc004Table = table.render({
                        elem: app.getByid(doc, 'gwdc004_edit1_table'),
                        id: 'gwdc004_edit1_table',
                        cols: cols1,
                        data: listData,
                        //toolbar: '#table-toolbar1',
                        defaultToolbar: ['exports_import'],
                        importsUrl: g_ctxPath + 'crm/gwdc004/importsData',
                        exportsMbUrl: g_ctxPath + 'files/促销价格批量导入.xlsx',
                        //loading: true,
                        patotalRow: true,
                        where: table_where,
                        done: function (res, curr, count) {
                            //self.reloadUpload('import');
                        }
                    });

                    table.on('tool(gwdc004_edit1_table)', function (obj) {
                        if (obj.event === 'openKh') {
                            if (vueData.form.GWDC007 == '2') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'cq_gwak001_4',
                                            columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            obj.update({
                                                'GWDC016': data.GWAK001,
                                                'GWDC017': data.GWAKL003
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwae001',
                                            columnList: [{label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                                {label: '说明', prop: 'GWAEL003', width: '150'},
                                                {label: '助记码', prop: 'GWAEL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            obj.update({
                                                'GWDC016': data.GWAE001,
                                                'GWDC017': data.GWAEL003
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择销售渠道信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        } else if (obj.event == 'openGwam') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'cq_gwam001_2',
                                        columnList: [{label: '产品编号', prop: 'GWAM001', width: '150'},
                                            {label: '品名', prop: 'GWAML003', width: '150'},
                                            {label: '规格', prop: 'GWAML004', width: '150'},
                                            {label: '单位', prop: 'GWAM008', width: '150'}
                                        ],
                                        dataList: [],
                                        key: 'ROW_ID'
                                    },
                                    cb: function (data) {
                                        var list = data;
                                        var listData = gwdc004Table.getDatas();
                                        var select = [];
                                        var _xdata = [];
                                        for (var i = 0; i < list.length; i++) {
                                            _xdata.push({
                                                GWDC007: vueData.form.GWDC007,
                                                GWDC016: obj.data.GWDC016,
                                                GWDC018: list[i].GWAM001,
                                                GWDC008: vueData.form.GWDC008
                                            })
                                        }
                                        if (vueData.form.GWDC007 == '1') {
                                            app.ajax({
                                                xurl: 'crm/gwdc004/getGwdb',
                                                xdata: _xdata,
                                                async: false,
                                                toJsonStr: true,
                                                cb: function (result) {
                                                    if (result.ok) {
                                                        for (var i = 0; i < result.data.data.length; i++) {
                                                            select.push({
                                                                'GWDC002': listData.length + i,
                                                                'GWDC016': obj.data.GWDC016,
                                                                'GWDC017': obj.data.GWDC017,
                                                                'GWDC018': result.data.data[i].GWAM001,
                                                                'GWAML003': result.data.data[i].GWAML003,
                                                                'GWAML004': result.data.data[i].GWAML004,
                                                                'GWDC019': result.data.data[i].GWAM008,
                                                                'GWDC023': result.data.data[i].GWDB016,
                                                                'GWDC024': ((result.data.data[i].GWDB016) * (vueData.form.GWDC027 / 100)).toFixed(2)
                                                            })
                                                        }
                                                        var unselect = [];
                                                        var arr1 = [];
                                                        var arr2 = [];
                                                        select.forEach(function (v) {
                                                            arr1.push(v.GWDC018);
                                                        });
                                                        _xdata.forEach(function (v) {
                                                            arr2.push(v.GWDC018);
                                                        });
                                                        arr1.concat(arr2).filter(function (item) {
                                                            if (!arr1.includes(item) || !arr2.includes(item)) {
                                                                unselect.push(item);
                                                            }
                                                        });
                                                        if (select.length > 0) {
                                                            listData.removeByMap({
                                                                GWDC002: obj.data.GWDC002
                                                            });
                                                            listData.push.apply(listData, select);
                                                        }
                                                        self.table1Reload(listData);
                                                        if (select.length != _xdata.length) {
                                                            var text = '';
                                                            unselect.forEach(function (v) {
                                                                text = text + v + ',';
                                                            });
                                                            app.showWarning('选择的商品' + text + '未设置结算价请检查!');
                                                        }
                                                    }
                                                }
                                            });
                                        } else if (vueData.form.GWDC007 == '2') {
                                            app.ajax({
                                                xurl: 'crm/gwdc004/getGwdb',
                                                xdata: _xdata,
                                                async: false,
                                                toJsonStr: true,
                                                cb: function (result) {
                                                    if (result.ok) {
                                                        for (var i = 0; i < result.data.data.length; i++) {
                                                            select.push({
                                                                'GWDC002': listData.length + i,
                                                                'GWDC016': obj.data.GWDC016,
                                                                'GWDC017': obj.data.GWDC017,
                                                                'GWDC018': result.data.data[i].GWAM001,
                                                                'GWAML003': result.data.data[i].GWAML003,
                                                                'GWAML004': result.data.data[i].GWAML004,
                                                                'GWDC019': result.data.data[i].GWAM008,
                                                                'GWDC023': result.data.data[i].GWDB016,
                                                                'GWDC024': ((result.data.data[i].GWDB016) * (vueData.form.GWDC027 / 100)).toFixed(2)
                                                            })
                                                        }
                                                        var unselect = [];
                                                        var arr1 = [];
                                                        var arr2 = [];
                                                        select.forEach(function (v) {
                                                            arr1.push(v.GWDC018);
                                                        });
                                                        _xdata.forEach(function (v) {
                                                            arr2.push(v.GWDC018);
                                                        });
                                                        arr1.concat(arr2).filter(function (item) {
                                                            if (!arr1.includes(item) || !arr2.includes(item)) {
                                                                unselect.push(item);
                                                            }
                                                        });
                                                        if (select.length > 0) {
                                                            listData.removeByMap({
                                                                GWDC002: obj.data.GWDC002
                                                            });
                                                            listData.push.apply(listData, select);
                                                        }
                                                        self.table1Reload(listData);
                                                        if (select.length != _xdata.length) {
                                                            var text = '';
                                                            unselect.forEach(function (v) {
                                                                text = text + v + ',';
                                                            });
                                                            app.showWarning('选择的商品' + text + '未设置结算价请检查!');
                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择品号信息',
                                    area: ['600px', '500px']
                                }
                            });
                        }
                    });
                    //监听单元格编辑
                    table.on('edit(gwdc004_edit1_table)', function (obj) {
                        if (obj.field == 'GWDC024') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                                app.showError('活动价必需是数字');
                                obj.updateOfEdit({
                                    'GWDC024': ((obj.data.GWDC023) * (vueData.form.GWDC027 / 100)).toFixed(2)
                                });
                            } else {
                                if (obj.value <= 0 || obj.value > obj.data.GWDC023) {
                                    app.showError('活动价必需大于0且小于等于结算价');
                                    obj.updateOfEdit({
                                        'GWDC024': ((obj.data.GWDC023) * (vueData.form.GWDC027 / 100)).toFixed(2)
                                    });
                                } else {
                                    obj.updateOfEdit({
                                        'GWDC024': parseFloat(obj.value).toFixed(2)
                                    });
                                }
                            }

                        }
                    });
                });
            },
            initTable2: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdczTable = table.render({
                        elem: app.getByid(doc, 'gwdc004_edit2_table'),
                        id: 'gwdc004_edit2_table',
                        size: _layuiConfig.ui_size,
                        autoSort: false, //禁用前端自动排序
                        cols: cols2,
                        data: listData,
                        //loading: true,
                        page: false,
                        height: 200,
                        totalRow: true,
                        where: '',
                        done: function (res, curr, count) {
                        }
                    });
                    table.on('tool(gwdc004_edit2_table)', function (obj) {
                        if (obj.event == 'openGwdcz') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'cq_gwam001_2',
                                        columnList: [{label: '产品编号', prop: 'GWAM001', width: '150'},
                                            {label: '品名', prop: 'GWAML003', width: '150'},
                                            {label: '规格', prop: 'GWAML004', width: '150'},
                                            {label: '单位', prop: 'GWAM008', width: '150'}
                                        ],
                                        dataList: [],
                                        key: 'ROW_ID'
                                    },
                                    cb: function (data) {
                                        var list = data;
                                        var select = [];
                                        var _xdata = [];
                                        var gwdc004 = gwdc004Table.getDatas();
                                        var listData = gwdczTable.getDatas();
                                        for (var i = 0; i < list.length; i++) {
                                            _xdata.push({
                                                GWDC007: vueData.form.GWDC007,
                                                GWDC016: gwdc004[0].GWDC016,
                                                GWDC018: list[i].GWAM001,
                                                GWDC008: vueData.form.GWDC008
                                            })
                                        }
                                        if (vueData.form.GWDC007 == '1') {
                                            app.ajax({
                                                xurl: 'crm/gwdc004/getGwdb',
                                                xdata: _xdata,
                                                async: false,
                                                toJsonStr: true,
                                                cb: function (result) {
                                                    if (result.ok) {
                                                        for (var i = 0; i < result.data.data.length; i++) {
                                                            select.push({
                                                                'GWDCZ002': '0',
                                                                'GWDCZ003': listData.length + i,
                                                                'GWDCZ004': result.data.data[i].GWAM001,
                                                                'GWAML003': result.data.data[i].GWAML003,
                                                                'GWAML004': result.data.data[i].GWAML004,
                                                                'GWDCZ005': result.data.data[i].GWDB016,
                                                                'GWDCZ006': result.data.data[i].GWDB016
                                                            })
                                                        }
                                                        var unselect = [];
                                                        var arr1 = [];
                                                        var arr2 = [];
                                                        select.forEach(function (v) {
                                                            arr1.push(v.GWDCZ004);
                                                        });
                                                        _xdata.forEach(function (v) {
                                                            arr2.push(v.GWDC018);
                                                        });
                                                        arr1.concat(arr2).filter(function (item) {
                                                            if (!arr1.includes(item) || !arr2.includes(item)) {
                                                                unselect.push(item);
                                                            }
                                                        });
                                                        for (var i = 0; i < unselect.length; i++) {
                                                            list.forEach(function (v) {
                                                                if (v.GWAM001 == unselect[i]) {
                                                                    select.push({
                                                                        'GWDCZ002': '0',
                                                                        'GWDCZ003': select.length + 1,
                                                                        'GWDCZ004': v.GWAM001,
                                                                        'GWAML003': v.GWAML003,
                                                                        'GWAML004': v.GWAML004,
                                                                        'GWDCZ005': '0',
                                                                        'GWDCZ006': '0'
                                                                    })
                                                                }
                                                            })
                                                        }
                                                        if (select.length > 0) {
                                                            listData.removeByMap({
                                                                GWDCZ003: obj.data.GWDCZ003
                                                            });
                                                            listData.push.apply(listData, select);
                                                        }
                                                        self.table2Reload(listData);
                                                    }
                                                }
                                            });
                                        } else if (vueData.form.GWDC007 == '2') {
                                            app.ajax({
                                                xurl: 'crm/gwdc004/getGwdb',
                                                xdata: _xdata,
                                                async: false,
                                                toJsonStr: true,
                                                cb: function (result) {
                                                    if (result.ok) {
                                                        for (var i = 0; i < result.data.data.length; i++) {
                                                            select.push({
                                                                'GWDCZ002': '0',
                                                                'GWDCZ003': listData.length + i,
                                                                'GWDCZ004': result.data.data[i].GWAM001,
                                                                'GWAML003': result.data.data[i].GWAML003,
                                                                'GWAML004': result.data.data[i].GWAML004,
                                                                'GWDCZ005': result.data.data[i].GWDB016,
                                                                'GWDCZ006': result.data.data[i].GWDB016
                                                            })
                                                        }
                                                        var unselect = [];
                                                        var arr1 = [];
                                                        var arr2 = [];
                                                        select.forEach(function (v) {
                                                            arr1.push(v.GWDCZ004);
                                                        });
                                                        _xdata.forEach(function (v) {
                                                            arr2.push(v.GWDC018);
                                                        });
                                                        arr1.concat(arr2).filter(function (item) {
                                                            if (!arr1.includes(item) || !arr2.includes(item)) {
                                                                unselect.push(item);
                                                            }
                                                        });
                                                        for (var i = 0; i < unselect.length; i++) {
                                                            list.forEach(function (v) {
                                                                if (v.GWAM001 == unselect[i]) {
                                                                    select.push({
                                                                        'GWDCZ002': '0',
                                                                        'GWDCZ003': select.length + 1,
                                                                        'GWDCZ004': v.GWAM001,
                                                                        'GWAML003': v.GWAML003,
                                                                        'GWAML004': v.GWAML004,
                                                                        'GWDCZ005': '0',
                                                                        'GWDCZ006': '0'
                                                                    })
                                                                }
                                                            })
                                                        }
                                                        if (select.length > 0) {
                                                            listData.removeByMap({
                                                                GWDCZ003: obj.data.GWDCZ003
                                                            });
                                                            listData.push.apply(listData, select);
                                                        }
                                                        self.table2Reload(listData);
                                                    }
                                                }
                                            });
                                        }
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择品号信息',
                                    area: ['600px', '500px']
                                }
                            });
                        }
                    });
                    //监听单元格编辑
                    table.on('edit(gwdc004_edit2_table)', function (obj) {
                        if (obj.field == 'GWDCZ005') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                                app.showError('活动价必需是数字');
                                obj.updateOfEdit({
                                    'GWDCZ005': obj.data.GWDCZ006
                                });
                            } else {
                                if (obj.data.GWDCZ006 == 0) {
                                    if (obj.value < 0) {
                                        app.showError('活动价必需大于等于0');
                                        obj.updateOfEdit({
                                            'GWDCZ005': obj.data.GWDCZ006
                                        });
                                    } else {
                                        obj.updateOfEdit({
                                            'GWDCZ005': parseFloat(obj.value).toFixed(2)
                                        });
                                    }
                                } else {
                                    if (obj.value < 0 && obj.value > obj.data.GWDCZ006) {
                                        app.showError('活动价必需大于等于0且小于等于结算价');
                                        obj.updateOfEdit({
                                            'GWDCZ005': obj.data.GWDCZ006
                                        });
                                    } else {
                                        obj.updateOfEdit({
                                            'GWDCZ005': parseFloat(obj.value).toFixed(2)
                                        });
                                    }
                                }
                            }
                        }
                    });
                });
            },
            initTable3: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdctTable = table.render({
                        elem: app.getByid(doc, 'gwdc004_gwdct_table'),
                        id: 'gwdc004_gwdct_table',
                        cols: cols3,
                        data: listData,
                        page: false,
                        height: 200
                    });

                    //监听单元格编辑
                    table.on('edit(gwdc004_gwdct_table)', function (obj) {
                        if (obj.field == 'GWDCT003') {
                            if (!(/(^[1-9]\d*$)/.test(obj.value))) {
                                app.showError('起订数量必须为正整数！');
                                obj.updateOfEdit({
                                    'GWDCT003': ''
                                });
                            }
                        } else if (obj.field == 'GWDCT004') {
                            if (!(/^[0-9]+.?[0-9]*$/).test(obj.value)) {
                                app.showError('起订金额必须为数字！');
                                obj.updateOfEdit({
                                    'GWDCT004': ''
                                });
                            }
                            if (parseFloat(obj.value) <= 0) {
                                app.showError('起订金额必须大于0！');
                                obj.updateOfEdit({
                                    'GWDCT004': ''
                                });
                            }
                        } else if (obj.field == 'GWDCT005') {
                            var data = obj.data;
                            var GWDCT005 = obj.value;
                            if (data.GWDCT006 != null && data.GWDCT006 != "") {
                                app.showError('最大赠品数量与最大赠品金额只能输一个！');
                                obj.updateOfEdit({
                                    'GWDCT005': ''
                                });
                            } else {
                                if (!(/(^[1-9]\d*$)/.test(GWDCT005))) {
                                    app.showError('最大赠品数量必须为数字！');
                                    obj.updateOfEdit({
                                        'GWDCT005': ''
                                    });
                                }
                            }
                        } else if (obj.field == 'GWDCT006') {
                            var data = obj.data;
                            var GWDCT006 = obj.value;
                            if (data.GWDCT005 != null && data.GWDCT005 != "") {
                                app.showError('最大赠品数量与最大赠品金额只能输一个！');
                                obj.updateOfEdit({
                                    'GWDCT006': ''
                                });
                            } else {
                                if (!(/^[0-9]+.?[0-9]*$/).test(GWDCT006)) {
                                    app.showError('最大赠品金额必须为数字！');
                                    obj.updateOfEdit({
                                        'GWDCT006': ''
                                    });
                                }
                                if (parseFloat(GWDCT006) <= 0) {
                                    app.showError('最大赠品金额必须大于0！');
                                    obj.updateOfEdit({
                                        'GWDCT006': ''
                                    });
                                }
                            }
                        }
                    });
                });
            },
            table1Reload: function (data) {
                self._reloadTable({id: 'gwdc004_edit1_table', data: data});
            },
            table2Reload: function (data) {
                table.reload('gwdc004_edit2_table', {
                    data: data
                });
            },
            table3Reload: function (data) {
                gwdctTable = table.reload('gwdc004_gwdct_table', {
                    data: data
                });
            },
            addData: function (list1, list2, list3) {
                app.ajax({
                    xurl: 'crm/gwdc004/addData',
                    xdata: {list1: list1, list2: list2, list3: list3},
                    loading: true,
                    toJsonStr: true,
                    okmsg: '组合促销价格增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc004').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },
            editData: function (list1, list2, list3) {
                app.ajax({
                    xurl: 'crm/gwdc004/editData',
                    xdata: {list1: list1, list2: list2, list3: list3},
                    loading: true,
                    toJsonStr: true,
                    okmsg: '组合促销价格修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwdc004').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            reloadUpload: function (id) {
                layui.use('upload', function () {
                    upload = layui.upload;
                    var datas = {GWDC007: vueData.form.GWDC007, GWDC008: vueData.form.GWDC008};
                    upload.render({
                        elem: '#' + id,
                        url: 'crm/gwdc004/importsData',
                        accept: 'file',
                        acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        data: datas,
                        done: function (res, index, upload) {
                            if (res.code == 300) {
                                app.showMessage("上传成功");
                                var list = gwdc004Table.getDatas();
                                var count = list.length + 1;
                                for (var i = 0; i < res.data.length; i++) {
                                    list.push({
                                        'GWDC002': count + i,
                                        'GWDC016': res.data[i].GWDC016,
                                        'GWDC017': res.data[i].GWDC017,
                                        'GWDC018': res.data[i].GWDC018,
                                        'GWAML003': res.data[i].GWAML003,
                                        'GWAML004': res.data[i].GWAML004,
                                        'GWDC019': res.data[i].GWDC019,
                                        'GWDC023': res.data[i].GWDC023,
                                        'GWDC024': res.data[i].GWDC024
                                    })
                                }
                                listData = list;
                                self.table1Reload(listData);
                            } else {
                                app.showError(res.message);
                            }
                        },
                        error: function (index, upload) {
                            app.showError("上传失败");
                        }
                    });
                });
            },

            _call: function (tagData) {
                pageVue.$refs.form.resetFields();
                moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if (tagData.is_add) {
                    vueData.detail = false;
                    vueData.isAdd = true;
                    /*vueData.gwdc027 = false;*/
                    vueData.gwdc020 = true;
                    vueData.gwdc021 = true;
                    vueData.gwdcz = true;
                    vueData.gwdc007 = false;
                    vueData.gwdc025 = true;
                    vueData.gwdc026 = true;
                    vueData.GWDC022 = false;
                    app.ajax({
                        xurl: 'crm/gwdc004/getGwdc001',
                        cb: function (result) {
                            if (result.ok) {
                                table_where.GWDC007 = '1';
                                table_where.GWDC008 = result.data.data.GWDC008;
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWDC001: result.data.data.GWDC001,
                                        GWDC008: result.data.data.GWDC008,
                                        GWDC009: result.data.data.GWDC008,
                                        GWDC011: result.data.data.GWDC011,
                                        GWDC011_NAME: result.data.data.GWDC011_NAME,
                                        GWDC012: result.data.data.GWDC012,
                                        GWDC012_NAME: result.data.data.GWDC012_NAME,
                                        GWDC007: '1',
                                        GWDC004: '1',
                                        GWDC006: '1',
                                        GWDC025: null,
                                        GWDC026: null,
                                        GWDC027: 100
                                    },
                                    reset: true
                                });
                                listData = [];
                                self.table1Reload(listData);
                                self.table2Reload(listData);
                                self.table3Reload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    callObj = tagData.obj.data;
                    if (tagData.detail == true) {
                        vueData.detail = true;
                        vueData.gwdcz = true;
                        vueData.GWDC022 = true;
                    } else {
                        vueData.detail = false;
                        if (callObj.GWDC005 == '1') {
                            /* vueData.gwdc027 = true;*/
                            vueData.gwdc020 = false;
                            vueData.gwdc021 = false;
                            vueData.gwdcz = true;
                            vueData.gwdc025 = true;
                            vueData.gwdc026 = true;
                            vueData.GWDC022 = true;
                        } else if (callObj.GWDC005 == '2') {
                            vueData.gwdc021 = true;
                            vueData.gwdc020 = false;
                            /*vueData.gwdc027 = false;*/
                            vueData.gwdcz = true;
                            vueData.gwdc025 = true;
                            vueData.gwdc026 = true;
                            vueData.GWDC022 = true;
                        }
                        else if (callObj.GWDC005 == '3') {
                            vueData.gwdc021 = false;
                            vueData.gwdc020 = true;
                            /*vueData.gwdc027 = true;*/
                            vueData.gwdcz = false;
                            vueData.gwdc025 = false;
                            vueData.gwdc026 = true;
                            vueData.GWDC022 = false;
                        }
                        else if (callObj.GWDC005 == '4') {
                            vueData.gwdc021 = true;
                            vueData.gwdc020 = false;
                            /*vueData.gwdc027 = true;*/
                            vueData.gwdcz = false;
                            vueData.gwdc025 = false;
                            vueData.gwdc026 = false;
                            vueData.GWDC022 = false;
                        }
                    }
                    if (tagData.obj.data.GWDC022 == 'Y') {
                        vueData.hide = true;
                        app.ajax({
                            xurl: 'crm/gwdc004/listGwdct',
                            xdata: {GWDC001: callObj.GWDC001},
                            loading: true,
                            cb: function (result) {
                                listData = result.data.data;
                                self.table3Reload(listData);
                            }
                        });
                    } else {
                        vueData.hide = false;
                    }
                    app.ajax({
                        xurl: 'crm/gwdc004/listGwdc',
                        xdata: {GWDC001: callObj.GWDC001},
                        loading: true,
                        cb: function (result) {
                            listData = result.data.data;
                            self.table1Reload(listData);
                            if (listData.length > 0) {
                                vueData.gwdc007 = true;
                            }
                        }
                    });
                    app.ajax({
                        xurl: 'crm/gwdc004/listGwdcz',
                        xdata: {GWDC001: callObj.GWDC001},
                        loading: true,
                        cb: function (result) {
                            listData = result.data.data;
                            self.table2Reload(listData);
                        }
                    });
                    table_where.GWDC007 = vueData.form.GWDC007;
                    table_where.GWDC008 = vueData.form.GWDC008;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj,
                        reset: true
                    });
                }

            },
            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['crm_gwdc']);
                pageVue = new Vue({
                    el: '#gwdc004_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        change1: function (type) {
                            /*if (type != '2') {
                             vueData.form.GWDC027 = 100;
                             vueData.gwdc027 = true;
                             } else {
                             vueData.form.GWDC027 = '';
                             vueData.gwdc027 = false;
                             }*/
                            vueData.form.GWDC022 = 'N';
                            vueData.hide = false;
                            var list = [];
                            self.table3Reload(list);
                            if (type == '3') {
                                vueData.gwdc021 = false;
                                vueData.gwdc020 = true;
                                vueData.form.GWDC020 = '';
                            } else if (type == '2' || type == '4') {
                                vueData.gwdc020 = false;
                                vueData.gwdc021 = true;
                                vueData.form.GWDC021 = '';
                            } else {
                                vueData.gwdc020 = false;
                                vueData.gwdc021 = false;
                                vueData.form.GWDC020 = '';
                                vueData.form.GWDC021 = '';
                            }
                            if (type == '1' || type == '2') {
                                vueData.gwdcz = true;
                            } else {
                                vueData.gwdcz = false;
                            }
                            if (type == '1' || type == '2') {
                                vueData.gwdc025 = true;
                                vueData.GWDC022 = true;
                            } else if (type == '3' || type == '4') {
                                vueData.gwdc025 = false;
                                vueData.GWDC022 = false;
                            }
                        },
                        change2: function (val) {
                            var list = gwdc004Table.getDatas();
                            vueData.form.GWDC027 = parseFloat(val).toFixed(2);
                            list.forEach(function (v) {
                                v.GWDC024 = (v.GWDC023 * (val / 100)).toFixed(2);
                            });
                            self.table1Reload(list);
                        },
                        change3: function (val) {
                            if (!(/(^[1-9]\d*$)/.test(val))) {
                                vueData.form.GWDC020 = '';
                                app.showError('起订数量需输入正整数!');
                            } else {
                                if (val != null) {
                                    vueData.form.GWDC021 = null;
                                }
                            }
                        },
                        change4: function (val) {
                            if (!(/^[0-9]+.?[0-9]*$/).test(val)) {
                                val = '';
                                vueData.form.GWDC021 = '';
                                app.showError('起订金额需输入大于0的数字!');
                            } else {
                                if (val != null) {
                                    vueData.form.GWDC020 = null;
                                }
                            }
                        },
                        change5: function (val) {
                            if (!(/^[0-9]+.?[0-9]*$/).test(val)) {
                                val = '';
                                vueData.form.GWDC025 = null;
                                app.showWarning('最大赠品金额需输入大于0的数字!');
                            } else {
                                if (val != null) {
                                    vueData.form.GWDC026 = null;
                                }
                            }
                        },
                        change6: function (val) {
                            if (!(/(^[1-9]\d*$)/.test(val))) {
                                vueData.form.GWDC026 = null;
                                app.showWarning('最大赠品数量需输入正整数!');
                            } else {
                                if (val != null) {
                                    vueData.form.GWDC025 = null;
                                }
                            }
                        },
                        addData: function () {
                            var data = gwdc004Table.getDatas();
                            var count = data.length;
                            if (vueData.form.GWDC005 == '' || vueData.form.GWDC005 == null) {
                                app.showError('请先选择促销方式!');
                                return;
                            } else {
                                data.push({
                                    GWDC002: count + 1,
                                    GWDC016: '',
                                    GWDC017: '',
                                    GWDC018: '',
                                    GWAML003: '',
                                    GWAML004: '',
                                    GWDC019: '',
                                    GWDC023: '',
                                    GWDC024: ''
                                });
                                self.table1Reload(data);
                                if (data.length > 0) {
                                    vueData.gwdc007 = true;
                                }
                            }
                        },
                        addGwdcz: function () {
                            var list = gwdc004Table.getDatas();
                            if (list.length == 0) {
                                app.showError('请先选择商品信息!');
                            } else if (list[0].GWDC016 == '') {
                                app.showError('请先选择客户信息!');
                            } else {
                                var data = gwdczTable.getDatas();
                                var count = data.length;
                                data.push({
                                    GWDCZ002: '0',
                                    GWDCZ003: count + 1,
                                    GWDCZ004: '',
                                    GWAML003: '',
                                    GWAML004: '',
                                    GWDCZ005: '',
                                    GWDCZ006: ''
                                });
                                self.table2Reload(data);
                            }
                        },
                        cancle: function () {
                            // layer.close(layOpenIndex);
                            self._closeTpl();
                        },
                        del: function () {
                            var list = gwdc004Table.getDatas();
                            var list2 = gwdczTable.getDatas();
                            var checkStatus = table.checkStatus('gwdc004_edit1_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDC002: v.GWDC002
                                    });
                                    if (v.GWDC002 == '1') {
                                        list2 = [];
                                    }
                                });
                                if (list.length != 0) {
                                    var i = 1;
                                    list.forEach(function (v) {
                                        v.GWDC002 = i;
                                        i++;
                                    });
                                }
                                if (list.length == 0) {
                                    vueData.gwdc007 = false;
                                }
                                self.table1Reload(list);
                                self.table2Reload(list2);
                            }
                        },
                        delGwdcz: function () {
                            var list = gwdczTable.getDatas();
                            var checkStatus = table.checkStatus('gwdc004_edit2_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDCZ003: v.GWDCZ003
                                    });
                                });
                                var i = 1;
                                list.forEach(function (v) {
                                    v.GWDCZ003 = i;
                                    i++;
                                });
                                self.table2Reload(list);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var list1 = gwdc004Table.getDatas();
                                    var list2 = gwdczTable.getDatas();
                                    var list3 = gwdctTable.getDatas();
                                    var count_ = 0;
                                    var count_1 = 0;
                                    var count_2 = 0;
                                    if (vueData.form.GWDC022 == 'Y') {
                                        if (list3.length == 0) {
                                            app.showError('请维护起订数量、起定金额、最大赠品数量、最大赠品金额！');
                                            return;
                                        }
                                        for (var i = 0; i < list3.length; i++) {
                                            var v = list3[i];
                                            if (vueData.form.GWDC005 == '3') {
                                                if (v.GWDCT004 == null || v.GWDCT004 == '') {//起订金额为空
                                                    count_++;
                                                }
                                            } else if (vueData.form.GWDC005 == '4') {
                                                if (v.GWDCT003 == null || v.GWDCT003 == '') {//起订数量为空
                                                    count_++;
                                                }
                                            }
                                            if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {//最大赠送金额及最大赠送量任意一个必须有值!
                                                if ((v.GWDCT005 == null || v.GWDCT005 == '') && (v.GWDCT006 == null || v.GWDCT006 == '')) {
                                                    count_1++;
                                                }
                                            }
                                            if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                                                if ((v.GWDCT005 != null && v.GWDCT005 != '') && (v.GWDCT006 != null && v.GWDCT006 != '')) {
                                                    count_2++;
                                                }
                                            }
                                        }
                                    }
                                    if (vueData.form.GWDC022 == 'Y') {
                                        if (vueData.form.GWDC005 == '3' && count_ > 0) {
                                            app.showError('起订金额不可为空!');
                                            return;
                                        }
                                        if (vueData.form.GWDC005 == '4' && count_ > 0) {
                                            app.showError('起订数量不可为空!');
                                            return;
                                        }
                                        if (count_1 > 0) {
                                            app.showError('最大赠品数量和最大赠品金额必须输入一个!');
                                            return;
                                        }
                                        if (count_2 > 0) {
                                            app.showError('最大赠品数量和最大赠品金额只允许输入一个!');
                                            return;
                                        }
                                    }
                                    if (list1.length == 0) {
                                        app.showError('商品信息不可为空!');
                                        return;
                                    }
                                    if (vueData.form.GWDC005 == '3' && vueData.form.GWDC022 == 'N') {
                                        if (vueData.form.GWDC021 == null || vueData.form.GWDC021 == '') {
                                            app.showError('起订金额必输!');
                                            return;
                                        }
                                    } else if (vueData.form.GWDC005 == '2' || vueData.form.GWDC005 == '4') {
                                        if (vueData.form.GWDC022 == 'N') {
                                            if (vueData.form.GWDC020 == null || vueData.form.GWDC020 == '') {
                                                app.showError('起订数量必输!');
                                                return;
                                            }
                                        }
                                    }
                                    if (vueData.form.GWDC005 == '1') {
                                        if ((vueData.form.GWDC020 == null || vueData.form.GWDC020 == '') && (vueData.form.GWDC021 == null || vueData.form.GWDC021 == '')) {
                                            app.showWarning('起订金额和起订数量必须输入一个!');
                                            return;
                                        } else if ((vueData.form.GWDC020 != null && vueData.form.GWDC020 != '' ) && (vueData.form.GWDC021 != null && vueData.form.GWDC021 != '')) {
                                            app.showWarning('起订金额和起订数量只允许输入一个!');
                                            return;
                                        }
                                    }
                                    if (vueData.form.GWDC005 == '3' || vueData.form.GWDC005 == '4') {
                                        if (vueData.form.GWDC022 == 'N') {
                                            if ((vueData.form.GWDC025 == null || vueData.form.GWDC025 == '') && (vueData.form.GWDC026 == null || vueData.form.GWDC026 == '')) {
                                                app.showWarning('最大赠品数量和最大赠品金额必须输入一个!');
                                                return;
                                            } else if ((vueData.form.GWDC025 != null && vueData.form.GWDC025 != '') && (vueData.form.GWDC026 != null && vueData.form.GWDC026 != '')) {
                                                app.showWarning('最大赠品数量和最大赠品金额只允许输入一个!');
                                                return;
                                            }
                                        }
                                        if (list2.length == 0) {
                                            app.showError('赠品信息不可为空!');
                                            return;
                                        }
                                    }
                                    if (vueData.form.GWDC010 < vueData.form.GWDC009) {
                                        app.showError('截止日期不可小于生效日期!');
                                        return;
                                    }
                                    for (var i = 0; i < list1.length; i++) {
                                        var v = list1[i];
                                        if (v.GWDC016 == '' || v.GWDC016 == null
                                            || v.GWDC018 == '' || v.GWDC018 == null) {
                                            app.showWarning('请选择客户及商品!');
                                            return;
                                        }
                                    }
                                    for (var i = 0; i < list2.length; i++) {
                                        var v = list2[i];
                                        if (v.GWDCZ004 == '' || v.GWDCZ004 == null) {
                                            app.showWarning('请选择赠品!');
                                            return;
                                        }
                                    }
                                    list1.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.addData(list1, list2, list3);
                                    } else {
                                        self.editData(list1, list2, list3);
                                    }
                                }
                            });
                        },
                        openUser: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [{label: '人员编号', prop: 'ID', width: '150'},
                                            {label: '人员名称', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDC011_NAME = data.NAME;
                                        vueData.form.GWDC011 = data.ID;
                                        vueData.form.GWDC012_NAME = data.ORG_NAME;
                                        vueData.form.GWDC012 = data.ORG_ID;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择用户信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        addGwdct: function () {
                            var data = gwdctTable.getDatas();
                            var count = data.length;
                            data.push({
                                GWDCT002: count + 1,
                                GWDCT003: '',
                                GWDCT004: '',
                                GWDCT005: '',
                                GWDCT006: ''
                            });
                            if (vueData.form.GWDC005 == '3') {
                                gwdctTable.config.cols[0][2].edit = false;
                                gwdctTable.config.cols[0][3].edit = 'text';
                            } else if (vueData.form.GWDC005 == '4') {
                                gwdctTable.config.cols[0][2].edit = 'text';
                                gwdctTable.config.cols[0][3].edit = false;
                            }
                            self.table3Reload(data);
                        },
                        delGwdct: function () {
                            var list = gwdctTable.getDatas();
                            var checkStatus = table.checkStatus('gwdc004_gwdct_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWDCT002: v.GWDCT002
                                    });
                                });
                                if (list.length != 0) {
                                    var i = 1;
                                    list.forEach(function (v) {
                                        v.GWDCT002 = i;
                                        i++;
                                    });
                                }
                                self.table3Reload(list);
                            }
                        },
                        changeGwdc022: function (data) {
                            if (data == 'Y') {
                                vueData.hide = true;
                                vueData.form.GWDC027 = '100';
                                vueData.form.GWDC020 = '';
                                vueData.form.GWDC021 = '';
                                vueData.form.GWDC025 = '';
                                vueData.form.GWDC026 = '';
                            } else {
                                vueData.hide = false;
                            }
                            var list = [];
                            self.table3Reload(list);
                        },
                        change7: function () {
                            table_where.GWDC007 = vueData.form.GWDC007;
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwdc004_edit');
                    }
                });
                self.initTable1();
                self.initTable2();
                self.initTable3();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwdc004Edit();

})
;
